/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package digitaldreamers.libraries.games.clients.gameclient.gameui.implementation;

import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IAudio;
import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IFileIO;
import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IGame;
import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IMyGraphics;
import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IInput;
import digitaldreamers.libraries.games.clients.gameclient.gameui.abstracts.IScreenManager;
import java.awt.event.WindowListener;
import javax.swing.JFrame;

/**
 *
 * @author DaoLe
 */
public abstract class WindowsGame extends JFrame implements IGame, WindowListener {
    
    public static final int DEFAULT_WIDTH = 1000;
    public static final int DEFAULT_HEIGHT = 750;
    private static final String SCREEN_MANAGER_IS_NULL_ERROR_MESSAGE = "Screen manager must not be null.";
    
    protected RenderPanel m_RenderPanel = null;
    protected IMyGraphics m_MyGraphics = null;
    protected IAudio m_Audio = null;
    protected IInput m_Input = null;
    protected IFileIO m_FileIO = null;
    protected ScreenManager m_ScreenManager = null;
    protected boolean m_bIsSoundEnable = true;
    protected Object m_SavedData = null;
    
    public WindowsGame(ScreenManager screenManager) {
        this(DEFAULT_WIDTH, DEFAULT_HEIGHT, screenManager);
    }
    
    public WindowsGame(int iWidth, int iHeight, ScreenManager screenManager) {
        if(screenManager == null) {
             throw new IllegalArgumentException(SCREEN_MANAGER_IS_NULL_ERROR_MESSAGE);
        }
        m_ScreenManager = screenManager;
        setSize(iWidth, iHeight);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(true);
        m_RenderPanel = new RenderPanel(this);
        add(m_RenderPanel);
        m_ScreenManager.setRenderPanel(m_RenderPanel);
        addWindowListener(this);
    }

    @Override
    public void start() {
        m_ScreenManager.start();
        m_RenderPanel.resume();
    }
    
    @Override
    public void pause() {
        m_RenderPanel.pause();
        m_ScreenManager.pause();
    }
    
    @Override
    public void resume() {
        m_ScreenManager.resume();
        m_RenderPanel.resume();
    }
    
    @Override
    public void enableSound() {
        m_bIsSoundEnable = true;
    }

    @Override
    public void disableSound() {
        m_bIsSoundEnable = false;
    }

    @Override
    public boolean isSoundEnable() {
        return m_bIsSoundEnable;
    }

    @Override
    public IInput getInput() {
        return m_Input;
    }

    @Override
    public IFileIO getFileIO() {
        return m_FileIO;
    }

    @Override
    public IMyGraphics getMyGraphics() {
        return m_MyGraphics;
    }

    @Override
    public IAudio getAudio() {
        return m_Audio;
    }
    
    @Override
    public IScreenManager getScreenManager() {
        return m_ScreenManager;
    }

    @Override
    public boolean hasSavedData() {
        return m_SavedData != null;
    }

    @Override
    public void saveData(Object savedData) {
        m_SavedData = savedData;
    }

    @Override
    public Object getSavedData() {
        return m_SavedData;
    }
}
